@file:Suppress("CAST_NEVER_SUCCEEDS")

package me.leon.plugin

import com.android.build.gradle.BaseExtension
import com.android.build.gradle.LibraryExtension
import com.android.build.gradle.api.AndroidBasePlugin
import org.gradle.api.JavaVersion
import org.gradle.api.Project
import org.gradle.api.tasks.compile.JavaCompile
import org.gradle.kotlin.dsl.*
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

internal fun Project.configurePlugins() {
    plugins.apply("kotlin-android")
    plugins.apply("kotlin-parcelize")
//    plugins.apply("kotlin-kapt")
}

internal fun Project.configureAndroid() = this.extensions.findByType<BaseExtension>()?.run {
    compileSdkVersion(Build.compileSdk)

    defaultConfig {
        minSdkVersion(Build.minSdk)
        targetSdkVersion(Build.targetSdk)
        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
        consumerProguardFiles("consumer-rules.pro")
    }

    buildFeatures.viewBinding = true

    lintOptions {
        isAbortOnError = false
    }
    val elements = mutableListOf(
        "META-INF/NOTICE.txt",
        "META-INF/DEPENDENCIES",
        "META-INF/*android*",
        "okhttp3/**",
        "META-INF/ASL2.0",
        "META-INF/DEPENDENCIES",
        "META-INF/*kotlin*",
        "kotlin/**",
        "DebugProbesKt.bin",
    )
    this@configureAndroid.extensions.findByType<LibraryExtension>()?.run {
        println("configure lib $this")
        elements.remove("META-INF/*kotlin*")
    }

    packagingOptions { resources { excludes.addAll(elements) } }

    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }

    tasks.run {
        withType<KotlinCompile> {
            kotlinOptions.jvmTarget = "1.8"
        }
        withType<JavaCompile> {
            options.encoding = "UTF-8"
        }
    }
    testOptions {
        unitTests {
            isReturnDefaultValues = true
            isIncludeAndroidResources = true
        }
    }
}

internal fun Project.configureRepo() = repositories {
    maven("https://maven.aliyun.com/repository/public")
    maven("https://maven.aliyun.com/repository/google")
    maven("https://maven.aliyun.com/repository/gradle-plugin")
    mavenCentral()
    google()
    mavenLocal()
    maven("https://www.jitpack.io")
    maven("https://oss.sonatype.org/content/repositories/snapshots")
    maven("https://s1.oss.sonatype.org/content/repositories/snapshots")
    maven("https://download.01.org/crosswalk/releases/crosswalk/android/maven2")
    maven {
        setUrl("https://repo.rdc.aliyun.com/repository/73874-release-rj61fm/")
        credentials {
            username = "bG5xXG"
            password = "HTv3V8etOF"
        }
    }


}

const val implementation = "implementation"
const val debugImplementation = "debugImplementation"
const val testImplementation = "testImplementation"
const val androidTestImplementation = "androidTestImplementation"
internal fun Project.configureDependence() = dependencies {
    add(implementation, (fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar")))))
}

internal fun Project.containsAndroidPlugin(): Boolean {
    return project.plugins.toList().any { plugin -> plugin is AndroidBasePlugin }
}